<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author Sebastian
 */
class MD_User {

    const SESSION_USER_KEY = "SESSION_USER_KEY";

    public static function login($user, $md5Password) {
        $auth = Zend_Auth::getInstance();

        $authAdapter = new Zend_Auth_Adapter_DbTable(CMS_Db::getInstance(), 'user', 'login', 'password');

        $authAdapter->setIdentity($user)
                ->setCredential($md5Password);

        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            $auth->getStorage()->write($authAdapter->getResultRowObject());
        } else {
            $auth->clearIdentity();
        }

        return $result->isValid();
    }

    public static function logout() {
        self::clearIdentity();
    }

    public static function clearIdentity() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
    }

    public static function readStorage() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity())
            return $auth->getStorage()->read();
        else
            return null;
    }

    public static function hasIdentity() {
        $auth = Zend_Auth::getInstance();

        if ($auth->hasIdentity())
            return true;
        else
            return false;
    }

    public static function getCategory() {
        $id = self::readStorage()->id_category;
        if (!empty($id))
            return $id;
        else
            return 0;
    }

    public static function getId() {
        $id = self::readStorage()->id_user;
        if (!empty($id))
            return $id;
        else
            return 0;
    }

    public static function getIdFromToolcode($toolcode) {
        $sql = "SELECT p.fk_user
                FROM descriptionunit d
                JOIN physicalunit p ON p.id_PhysicalUnit = d.fk_PhysicalUnit
                WHERE d.toolcode = '$toolcode'";
        return TN_Db::getInstance()->fetchOne($sql);
    }

    public static function getUserName() {
        $data = self::readStorage();
        $name = $data->name;
        $surname = $data->surname;

        if (!empty($data))
            return $name . " " . $surname;
        else
            return "";
    }

    public static function getName() {
        $data = self::readStorage();
        $name = $data->name;


        if (!empty($data))
            return $name;
        else
            return "";
    }

    public static function getSurname() {
        $data = self::readStorage();

        $surname = $data->last_name;

        if (!empty($data))
            return $surname;
        else
            return "";
    }

    public static function getEmail() {
        $data = self::readStorage();

        $email = $data->email;

        if (!empty($data))
            return $email;
        else
            return "";
    }

    public static function getDecimalPoint() {
        return ",";
    }

    public static function getThousandsSeparator() {
        return ".";
    }

    public static function useThousandsSeparator() {
        return false;
    }

    /**
     * 
     * @param $email
     * @return boolean
     * Chequea que el email de un usuario exista en la DB
     */
    public static function exists($email) {
        $exist = CMS_Db::getInstance()->fetchOne("SELECT id_user FROM user WHERE email = '$email'");
        return $exist;
    }

    public static function signup($name, $surname, $email, $password) {
        $user_exist = CMS_Db::getInstance()->fetchOne("SELECT id_user FROM user WHERE email = '$email'");

        if (!($user_exist > 0)) {
            $bind = array("name" => $name, "surname" => $surname, "email" => $email, "password" => md5($password));
            if (CMS_Db::getInstance()->insert("user", $bind)) {
                $fk_user = CMS_Db::getInstance()->lastInsertId("user");
                CMS_Db::getInstance()->update("user", array("fk_user" => $fk_user), "id_user = $fk_user");
                return true;
            }
        }

        return false;
    }

    public static function changePassword($newpassword) {
        if (self::hasIdentity()) {
            CMS_Db::getInstance()->update('user', array('password' => md5($newpassword)), "id = " . self::getId());
            return true;
        }

        return false;
    }

    public static function recoveryPassword($email) {
        $newpassword = Common_System_Code::getSimpleCode(8);
        CMS_Db::getInstance()->update('user', array('password' => md5($newpassword)), "email = '$email'");
        return $newpassword;
    }

}

?>